package com.yuba.web.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.yuba.common.core.config.YubaConfig;
import com.yuba.common.core.domain.R;
import com.yuba.common.core.utils.StringUtils;
import com.yuba.common.json.utils.JsonUtils;
import com.yuba.common.satoken.utils.LoginHelper;
import com.yuba.common.sse.dto.SseMessageDto;
import com.yuba.common.sse.utils.SseMessageUtils;
import com.yuba.front.domain.bo.TagsBo;
import com.yuba.front.domain.vo.TagHotVo;
import com.yuba.front.domain.vo.TagsVo;
import com.yuba.front.event.handle.MessageEventHandler;
import com.yuba.front.service.ITagsService;
import com.yuba.system.domain.bo.SysDictDataBo;
import com.yuba.system.domain.vo.SysDictDataVo;
import com.yuba.system.service.ISysDictDataService;
import lombok.RequiredArgsConstructor;
import org.dromara.sms4j.api.SmsBlend;
import org.dromara.sms4j.core.factory.SmsFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页
 *
 * @author Lion Li
 */

@RequiredArgsConstructor
@RestController
public class IndexController {

    /**
     * 系统基础配置
     */
    private final YubaConfig yubaConfig;
    private final ITagsService tagsService;
    private final ISysDictDataService dictDataService;

    /**
     * 访问首页，提示语
     */
    @SaIgnore
    @GetMapping("/")
    public String index() {
        return StringUtils.format("欢迎使用{}后台管理框架，当前版本：v{}，请通过前端地址访问。", yubaConfig.getName(), yubaConfig.getVersion());
    }

    /**
     * 获取关于标签的所有数据
     *      例如：全部标签、热门标签等
     */
    @SaIgnore // 放行请求
    @GetMapping("/api/common/get/all_select")
    public R<Map<String, Object>> getAllSelect() {
        Map<String, Object> data = new HashMap<>();

        // 查询 所有标签数据数据
        List<String> tagAll =  tagsService.queryList(new TagsBo())
            .stream()
            .map(TagsVo::getTagName)
            .toList();
        data.put("allTagList", tagAll);

        // 查询热门标签列表
        List<String> tagHotList = tagsService.getHot().stream().map(TagHotVo::getTagName).toList();
        data.put("hotTagList", tagHotList);

        // 查询知识类型列表
        SysDictDataBo sysDictDataBo = new SysDictDataBo();
        sysDictDataBo.setDictType("articles_category_code");
        List<SysDictDataVo> voList = dictDataService.selectDictDataList(sysDictDataBo);

        data.put("articlesCategoryList", voList);
        return R.ok(data);
    }

    /**
     * 检查登录状态
     * */
    @GetMapping("/api/common/checkLogin")
    public R<Void> checkLogin() {
        return R.ok();
    }

    @SaIgnore
    @GetMapping("/sms/test")
    public R<Void> tesSms() {
        LinkedHashMap<String, String> map = new LinkedHashMap<>(1);
        map.put("code", "1234");

        SmsBlend smsBlend = SmsFactory.getSmsBlend("config1");
        smsBlend.sendMessage("15240685114", "SMS_290871401", map);

        return R.ok();
    }

    @GetMapping("/sse/test")
    public R<Void> testSse() {
        SseMessageDto sseMessageDto = new SseMessageDto();
        sseMessageDto.setUserIds(List.of(LoginHelper.getUserId()));
        MessageEventHandler.MessageResponse messageResponse = new MessageEventHandler.MessageResponse("LIKE", "测试", true);
        sseMessageDto.setMessage(JsonUtils.toJsonString(messageResponse));
        SseMessageUtils.sendMessage(LoginHelper.getUserId(), JsonUtils.toJsonString(messageResponse));
//        SseMessageUtils.publishMessage(sseMessageDto);
        return R.ok();
    }
}
